<template>
  <a-modal :open="open" width="850px" title="检验单项目维护" centered :ok-button-props="{ disabled: !isCompleted }" @ok="onOk" @cancel="onCancel">
    <a-form :model="formState" class="form-style-flex" :label-col="{ style: { width: '100px' } }">
      <a-form-item label="项目代码" class="w-1/2" v-bind="validateInfos.itemCode">
        <a-input v-model:value="formState.itemCode" placeholder="请输入" />
      </a-form-item>
      <a-form-item class="w-1/2 pl-10">
        <a-radio-group v-model:value="formState.itemType">
          <a-radio value="1">数值型</a-radio>
          <a-radio value="2">文字型</a-radio>
          <a-radio value="3">阴阳型</a-radio>
        </a-radio-group>
      </a-form-item>
      <a-form-item label="项目名称" class="w-full" v-bind="validateInfos.itemName">
        <a-input v-model:value="formState.itemName" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="顺序号" class="w-1/4">
        <a-input-number v-model:value="formState.outorder" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="通道号" class="w-1/4">
        <a-input v-model:value="formState.channel" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="输出格式" class="w-1/4">
        <a-input v-model:value="formState.outputFormat" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="单位" class="w-1/4">
        <a-input v-model:value="formState.unit" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="参考范围(男)" class="w-1/2">
        <a-input-number v-model:value="formState.maleMinValue" class="w-[144px]" placeholder="请输入" />~
        <a-input-number v-model:value="formState.maleMaxValue" class="w-[143px]" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="参考范围(女)" class="w-1/2">
        <a-input-number v-model:value="formState.femaleMinValue" class="w-[144px]" placeholder="请输入" />~
        <a-input-number v-model:value="formState.femaleMaxValue" class="w-[143px]" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="参考值(男)" class="w-1/2">
        <a-input v-model:value="formState.maleReferValue" disabled placeholder="请输入" />
      </a-form-item>
      <a-form-item label="参考值(女)" class="w-1/2">
        <a-input v-model:value="formState.femaleReferValue" disabled placeholder="请输入" />
      </a-form-item>

      <a-form-item label="危急范围(男)" class="w-1/2">
        <a-input-number v-model:value="formState.maleCrisisMinValue" class="w-[144px]" placeholder="请输入" />~
        <a-input-number v-model:value="formState.maleCrisisMaxValue" class="w-[143px]" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="危急范围(女)" class="w-1/2">
        <a-input-number v-model:value="formState.femaleCrisisMinValue" class="w-[144px]" placeholder="请输入" />~
        <a-input-number v-model:value="formState.femaleCrisisMaxValue" class="w-[143px]" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="危急值(男)" class="w-1/2">
        <a-input v-model:value="formState.maleCrisisReferValue" disabled placeholder="请输入" />
      </a-form-item>
      <a-form-item label="危急值(女)" class="w-1/2">
        <a-input v-model:value="formState.femaleCrisisReferValue" disabled placeholder="请输入" />
      </a-form-item>

      <a-form-item label="上箭头" class="w-1/4">
        <a-input-number v-model:value="formState.upperTips" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="下箭头" class="w-1/4">
        <a-input-number v-model:value="formState.lowerTips" placeholder="请输入" />
      </a-form-item>
      <!-- <div class="w-1/2 relative">
        <a-checkbox v-model:checked="formState.checked" class="absolute top-1.5 left-3.5" />
        <a-form-item label="质控批号" class="w-full">
          <a-input v-model:value="formState.batchNumber" placeholder="请输入" />
        </a-form-item>
      </div> -->
      <a-form-item label="上灰区" class="w-1/2">
        <a-input v-model:value="formState.upperArea" placeholder="请输入" />
      </a-form-item>
      <a-form-item label="下灰区" class="w-1/2">
        <a-input v-model:value="formState.lowerArea" placeholder="请输入" />
      </a-form-item>
      <!-- <a-form-item label="公式" class="w-full">
        <a-input v-model:value="formState.formula" placeholder="请输入" />
      </a-form-item> -->
      <a-form-item label="备注" class="w-full">
        <a-textarea v-model:value="formState.memo" :row="4" placeholder="请输入" />
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script setup>
import { ref, watch } from 'vue'
import { message } from 'ant-design-vue'
import { post } from '@/utils/request'

const emit = defineEmits(['update:open', 'success'])
const props = defineProps({
  open: Boolean,
  rowData: {
    type: Object,
    default: () => ({})
  }
})

const formState = ref({ checked: false, itemCode: '', itemName: '' })

const rulesState = ref({
  itemCode: [{ required: true, message: '请输入项目代码' }],
  itemName: [{ required: true, message: '请输入项目名称' }]
})

const { isCompleted, validateInfos } = useForm(formState, rulesState)

const onOk = async () => {
  const { code, msg } = await post('/lis/InstrumentType/doSaveLisItem', formState.value)
  if (code !== 0) return message.error(msg)
  message.success(msg)
  emit('success')
  emit('update:open', false)
}

const onCancel = () => {
  emit('update:open', false)
}

watch(
  () => props.open,
  (val) => {
    if (val) {
      formState.value = { ...props.rowData, itemType: props.rowData?.id ? props.rowData?.itemType : '1' }
    }
  },
  {
    immediate: true
  }
)
// watch(
//   () => [formState.value.maleMinValue, formState.value.maleMaxValue, formState.value.unit],
//   (data) => {
//     formState.value.maleReferValue = `${data[0]||''}${data[2]}~${data[1]||''}${data[2]}`
//   }
// )

// watch(
//   () => [formState.value.femaleMinValue, formState.value.femaleMaxValue, formState.value.unit],
//   (data) => {
//     formState.value.femaleReferValue = `${data[0]||''}${data[2]}~${data[1]||''}${data[2]}`
//   }
// )
watch(
  () => [formState.value.maleMinValue, formState.value.maleMaxValue, formState.value.unit],
  (data) => {
    if (data && !formState.value.maleReferValue) {
      formState.value.maleReferValue = ((data[0] && data[0] + '') || '') + ((data[1] && '~' + data[1] + '') || '') + '(' + ((data[1] && data[2]) || '') + ')'
    }
  }
)

watch(
  () => [formState.value.femaleMinValue, formState.value.femaleMaxValue, formState.value.unit],
  (data) => {
    if (data && !formState.value.femaleReferValue) {
      formState.value.femaleReferValue = ((data[0] && data[0] + '') || '') + ((data[1] && '~' + data[1] + '') || '') + '(' + ((data[1] && data[2]) || '') + ')'
    }
  }
)

watch(
  () => [formState.value.maleCrisisMinValue, formState.value.maleCrisisMaxValue, formState.value.unit],
  (data) => {
    formState.value.maleCrisisReferValue = `<${data[0] || ''}${data[2]}或>${data[1] || ''}${data[2]}`
  }
)
watch(
  () => [formState.value.femaleCrisisMinValue, formState.value.femaleCrisisMaxValue, formState.value.unit],
  (data) => {
    formState.value.femaleCrisisReferValue = `<${data[0] || ''}${data[2]}或>${data[1] || ''}${data[2]}`
  }
)
</script>
